package com.ruoyi.api;

import com.alibaba.druid.sql.ast.statement.SQLIfStatement;
import com.ruoyi.common.constant.ApiConstants;
import com.ruoyi.common.dto.AppController;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.OrderUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.interceptor.annotation.IgnoreAuth;
import com.ruoyi.framework.interceptor.annotation.LoginUserId;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.project.hidden.hiddenLink.domain.HiddenLink;
import com.ruoyi.project.hidden.hiddenLink.service.IHiddenLinkService;
import com.ruoyi.project.hidden.hiddenName.domain.HiddenName;
import com.ruoyi.project.hidden.hiddenName.service.IHiddenNameService;
import com.ruoyi.project.hidden.hiddenOrder.domain.HiddenOrder;
import com.ruoyi.project.hidden.hiddenOrder.service.IHiddenOrderService;
import com.ruoyi.project.hidden.hiddenRepair.domain.HiddenRepair;
import com.ruoyi.project.hidden.hiddenRepair.service.IHiddenRepairService;
import com.ruoyi.project.hidden.hiddenSys.domain.HiddenSys;
import com.ruoyi.project.hidden.hiddenSys.service.IHiddenSysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ywh
 * @date 2021/1/26
 * @desc 隐患管理
 */
@RestController
@RequestMapping("/api/hidden")
public class ApiHiddenController extends AppController {

    @Autowired
    private IHiddenOrderService orderService;

    @Autowired
    private IHiddenSysService sysService;
    @Autowired
    private IHiddenLinkService linkService;
    @Autowired
    private IHiddenNameService nameService;
    @Autowired
    private IHiddenRepairService repairService;

    /*隐患系统*/
    @RequestMapping("/querySys")
    @IgnoreAuth
    public Object querySys() {
        HiddenSys sys = new HiddenSys();
        sys.setStatus("0");
        List<HiddenSys> list = sysService.selectHiddenSysList(sys);
        return writeResultRep(list);
    }

    /*隐患环节*/
    @RequestMapping("/queryLinkBySysId")
    @IgnoreAuth
    public Object queryLinkBySysId(Long sysId) {
        if (StringUtils.isNull(sysId)) {
            return toResponsFail("缺少参数");
        }
        HiddenLink link = new HiddenLink();
        link.setStatus("0");
        link.setSysId(sysId);
        List<HiddenLink> list = linkService.selectHiddenLinkList(link);
        return writeResultRep(list);
    }

    /*风险名称*/
    @RequestMapping("/queryNameByLinkId")
    @IgnoreAuth
    public Object queryNameByLinkId(Long linkId) {
        if (StringUtils.isNull(linkId)) {
            return toResponsFail("缺少参数");
        }
        HiddenName name = new HiddenName();
        name.setLinkId(linkId);
        List<HiddenName> list = nameService.selectHiddenNameList(name);
        return writeResultRep(list);
    }

    /*待指派 待整改 待验收 展示待处理数量*/
    @RequestMapping("/needUpdateCount")
    public Object needUpdateCount(@LoginUserId Long userId) {
        Map<String, Integer> map = new HashMap<>();
        //待指派
        Integer i = orderService.queryAuditNum(userId);
        map.put("auditNum", i);
        //待整改
        Integer j = repairService.queryUnHandleNum(userId);
        map.put("unHandleNum", j);
        //待验收
        Integer k = repairService.queryUnCheckNum(userId);
        map.put("unCheckNum", k);
        return writeResultRep(map);
    }


    /*隐患上报*/
    @RequestMapping("/toOrder")
    @RepeatSubmit
    public Object toOrder(@LoginUserId Long userId, HiddenOrder order) {
        if (StringUtils.isNull(order) || StringUtils.isNull(order.getRiskId())) {
            return toResponsFail("缺少参数");
        }
        order.setOrderNum(OrderUtil.getGoodsOrderNo());
        order.setUserId(userId);
        orderService.insertHiddenOrder(order);
        return writeResultRep();
    }

    /*我的上报记录*/
    @RequestMapping("/queryOrderList")
    public Object queryOrderList(@LoginUserId Long userId, Integer pageNum, Integer pageSize) {
        if (StringUtils.isNull(pageNum) || StringUtils.isNull(pageSize)) {
            return toResponsFail("缺少分页参数");
        }
        HiddenOrder order = new HiddenOrder();
        order.setUserId(userId);
        startPage();
        List<HiddenOrder> list = orderService.selectHiddenOrderList(order);
        return writeResultRep(getDataTable(list));
    }

    /*需要派单列表*/
    @RequestMapping("/queryAuditList")
    public Object queryAuditList(@LoginUserId Long userId, Integer pageNum, Integer pageSize) {
        if (StringUtils.isNull(pageNum) || StringUtils.isNull(pageSize)) {
            return toResponsFail("缺少分页参数");
        }
        HiddenOrder order = new HiddenOrder();
        order.setDutyId(userId);
        order.setStatus(ApiConstants.DAI_FEN_PEI);
        startPage();
        List<HiddenOrder> list = orderService.selectHiddenOrderList(order);
        return writeResultRep(getDataTable(list));
    }

    /*隐患详情---->*/
    @RequestMapping("/queryOrderById")
    @IgnoreAuth
    public Object queryOrderById(Long orderId) {
        HiddenOrder order = orderService.selectHiddenOrderById(orderId);
        HiddenRepair repair = repairService.queryRepairByOrderId(orderId);
        order.setRepair(repair);
        return writeResultRep(order);
    }

    /*分配订单*/
    @RequestMapping("/dispatchOrder")
    @RepeatSubmit
    public Object dispatchOrder(@LoginUserId Long userId, Long orderId, Long processor, Date alterTime) {
        if (StringUtils.isNull(orderId) || StringUtils.isNull(processor) || StringUtils.isNull(alterTime)) {
            return toResponsFail("缺少参数");
        }
        HiddenOrder order = orderService.selectHiddenOrderById(orderId);
        if (StringUtils.isNull(order) || !userId.equals(order.getDutyId()) || !order.getStatus().equals(ApiConstants.DAI_FEN_PEI) ) {
            return toResponsFail("分配信息有误，请刷新");
        }
        HiddenRepair repair = new HiddenRepair();
        repair.setOrderId(orderId);
        repair.setProcessor(processor);
        repair.setAlterDate(alterTime);
        int i = repairService.insertHiddenRepair(repair);
        if (i > 0) {
            order.setStatus(ApiConstants.CHU_LI_ZHONG);
            orderService.updateHiddenOrder(order);
            return writeResultRep();
        }
        return toResponsFail("分配失败");
    }

    /*已经派单列表*/
    @RequestMapping("/queryDispatchList")
    public Object queryDispatchList(@LoginUserId Long userId, Integer pageNum, Integer pageSize) {
        if (StringUtils.isNull(pageNum) || StringUtils.isNull(pageSize)) {
            return toResponsFail("缺少分页参数");
        }
        startPage();
        List<HiddenOrder> list = orderService.queryDispatchList(userId);
        return writeResultRep(getDataTable(list));
    }

    /*需要处理列表*/
    @RequestMapping("/queryUnHandleList")
    public Object queryUnHandleList(@LoginUserId Long userId, Integer pageNum, Integer pageSize) {
        if (StringUtils.isNull(pageNum) || StringUtils.isNull(pageSize)) {
            return toResponsFail("缺少分页参数");
        }
        startPage();
        List<HiddenRepair> list = repairService.queryUnHandleList(userId);
        return writeResultRep(getDataTable(list));
    }

    /*处理详情*/
    @RequestMapping("/toRepairDetail")
    @IgnoreAuth
    public Object toOrderDetail(Long repairId) {
        HiddenRepair repair = repairService.selectHiddenRepairById(repairId);
        return writeResultRep(repair);
    }


    /*提交处理*/
    @RequestMapping("/toHandleRepair")
    @RepeatSubmit
    public Object toHandleRepair(@LoginUserId Long userId, HiddenRepair repair) {
        if (StringUtils.isNull(repair) || StringUtils.isNull(repair.getRepairId()) ) {
            return toResponsFail("缺少参数");
        }
        HiddenRepair item = repairService.selectHiddenRepairById(repair.getRepairId());
        if (item .getValidFlag().equals(ApiConstants.DAI_YAN_SHOU) || item .getValidFlag().equals(ApiConstants.DAI_YAN_SHOU)
                || !userId.equals(item.getProcessor())){
            return toResponsFail("处理信息有误，请刷新");
        }
        repair.setValidFlag(ApiConstants.DAI_YAN_SHOU);
        repair.setSubmitTime(DateUtils.getNowDate());
        int i = repairService.updateHiddenRepair(repair);
        if (i>0){
            return toResponsMsgSuccess("提交成功");
        }
        return toResponsFail("提交失败");
    }

    /*需要验收列表*/
    @RequestMapping("/queryUnCheckList")
    public Object queryAcceptList(@LoginUserId Long userId, Integer pageNum, Integer pageSize) {
        if (StringUtils.isNull(pageNum) || StringUtils.isNull(pageSize)) {
            return toResponsFail("缺少分页参数");
        }
        HiddenRepair repair = new HiddenRepair();
        repair.setChecker(userId);
        repair.setValidFlag(ApiConstants.DAI_YAN_SHOU);
        startPage();
        List<HiddenRepair> list = repairService.selectHiddenRepairList(repair);
        return writeResultRep(getDataTable(list));
    }


    /*进行验收(是否通过)  需要加入当前时间*/
    @RequestMapping("/toCheckRepair")
    @RepeatSubmit
    @Transactional
    public Object toCheckRepair(@LoginUserId Long userId, HiddenRepair repair) {
        if (StringUtils.isNull(repair) || StringUtils.isNull(repair.getRepairId()) ||
                StringUtils.isEmpty(repair.getValidFlag()) || StringUtils.isEmpty(repair.getRemark())) {
            return toResponsFail("缺少参数");
        }
        HiddenRepair item = repairService.selectHiddenRepairById(repair.getRepairId());
        if (!item.getValidFlag().equals(ApiConstants.DAI_YAN_SHOU) || !userId.equals(item.getChecker())) {
            return toResponsFail("验收信息有误，请刷新");
        }
        if (ApiConstants.PASS.equals(repair.getValidFlag())) {  //通过
            HiddenOrder order = new HiddenOrder();
            order.setOrderId(item.getOrderId());
            order.setStatus(ApiConstants.PASS);


            orderService.updateHiddenOrder(order);
        } else if (ApiConstants.REBUT.equals(repair.getValidFlag())) {  //驳回


        } else {
            return toResponsFail("传参验收状态值有误");
        }
        repair.setCheckTime(new Date());
        repairService.updateHiddenRepair(repair);
        return toResponsMsgSuccess("提交成功");
    }

    /*已验收列表*/
    @RequestMapping("/queryHadCheckList")
    public Object queryHadCheckList(@LoginUserId Long userId, Integer pageNum, Integer pageSize) {
        if (StringUtils.isNull(pageNum) || StringUtils.isNull(pageSize)) {
            return toResponsFail("缺少分页参数");
        }
        HiddenRepair repair = new HiddenRepair();
        repair.setChecker(userId);
        repair.setValidFlag(ApiConstants.PASS);
        startPage();
        List<HiddenRepair> list = repairService.selectHiddenRepairList(repair);
        return writeResultRep(getDataTable(list));
    }


}
